Skip to content

mediamtx: substitute hostname/IP before start#977

Open
babo989 wants to merge 2 commits into
mainfrom
fix/mediamtx-reconfigure-on-start
Open

mediamtx: substitute hostname/IP before start#977
babo989 wants to merge 2 commits into
mainfrom
fix/mediamtx-reconfigure-on-start

Conversation

@babo989

@babo989 babo989 commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

An error I found in testing this week. This is to fix the silent WebRTC failure that causes the dashboard preview iframe to spin forever after any reset or redeploy of
`/etc/mediamtx.yaml`.

Root cause

`os/mediamtx/mediamtx.yaml` ships with placeholders in `webrtcAdditionalHosts`:

```yaml
webrtcAdditionalHosts: [
192.0.2.1, # placeholder IP
raspberrypi, # placeholder hostname
raspberrypi.local, # placeholder hostname.local
...
]
```

These are runtime-substituted by `lib/mediamtx.js`, called from the backend's `network.js` whenever its
`wired_controller` observable emits. If the substitution hasn't happened (boot race, image redeploy, manual revert,
config reset), MediaMTX advertises `raspberrypi` in WebRTC ICE candidates. Browsers try to resolve it, get `no such
host`, and silently fail peer-connection negotiation — the dashboard preview just shows an infinite spinner with no
visible error.

The failure surfaces in mediamtx logs as:
```
INF [WebRTC] [session ...] closed: lookup raspberrypi on :53: no such host
```

…but HTTP-level probes (curl on `/cam/`, `/cam/index.m3u8`) all return normal content, so the bug is invisible to
anything but a real browser doing WebRTC ICE.

Fix

Run `lib/mediamtx.js` as `ExecStartPre` so the YAML is always rewritten with the device's real hostname/IP before
MediaMTX loads it. Also bump the unit's network dependency from `network.target` to `network-online.target` so
`getWiredAddress()` can read the IP when the script runs.

No change needed to `lib/mediamtx.js` — its trailing IIFE already gates on `import.meta.main`, which Node 24 (the
version we ship) supports natively. Invoking the file directly runs the substitution and exits.

The shipped mediamtx.yaml uses placeholder values (raspberrypi, 192.0.2.1) for webrtcAdditionalHosts that lib/mediamtx.js is supposed to replace at runtime. Previously the substitution only ran on the backend's wired_controller observable, so any reset or redeploy of /etc/mediamtx.yaml left the placeholders advertised in WebRTC ICE candidates. Browsers cannot resolve "raspberrypi", silently fail peer-connection negotiation, and the dashboard preview spins forever with no visible error.

Run lib/mediamtx.js as ExecStartPre so every mediamtx start unconditionally substitutes the real values into the file before mediamtx loads it. Also order the unit after network-online.target so the IP detection in getWiredAddress() is valid when the script runs.

Verified on Node 24.15 (the version that ships): the IIFE at the bottom of lib/mediamtx.js gates on import.meta.main, which Node now supports natively, so no source change to lib/mediamtx.js is needed.

@sonnyp sonnyp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, could you test using runOnInit instead so it also works when started via other means than systemd; for example using just dev

Run lib/mediamtx.js from the cam path's runOnInit hook in mediamtx.yaml
rather than a systemd ExecStartPre, so the hostname/IP substitution also
runs when mediamtx is started outside systemd (e.g. `just dev`).

mediamtx watches the config file and hot-reloads after the rewrite;
runOnInit fires once because lib/mediamtx.js only edits the global
webrtcAdditionalHosts, leaving the cam path config unchanged. Keep
network-online.target so the wired IP is available when it runs.
@babo989

babo989 commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator Author

ammended-mediamtx: use runOnInit instead of ExecStartPre for yaml substitution

@sonnyp sonnyp changed the title mediamtx: substitute hostname/IP into yaml before start mediamtx: substitute hostname/IP before start Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants